package com.lkm.app_ui.ui.fj;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.m.fun.OnKeyNumberEvent;
import com.lkm.app_ui.view.choiceCard.ChoiceCard;
import com.lkm.app_ui.view.choiceCard.ChoiceItem;
import com.lkm.app_ui.view.choiceCard.IChoiceItem;
import com.lkm.comlib.help.ViewHelp;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;



public class FjWaitOrderFragment extends MBaseFragment {


    private FunView funView;

    @Override
    protected int configLayoutResID() {
        return R.layout.fj_fragment_waitorder;
    }

    @Override
    protected boolean configIsLockKey() {
        return true;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        funView = new FunView();
        binfjInfo();
    }



    private void binfjInfo() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnFjInfoEvent>() {
            @Override
            public void onReceiver(OnFjInfoEvent data) {
                FjWaitOrder fjWaitOrder = FjApp.install.getFjWaitOrder();

                funView.setFjOrders(fjWaitOrder.getFjOrders());
            }

            @Override
            public Class<OnFjInfoEvent> getBusEventClass() {
                return OnFjInfoEvent.class;
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }


    private class FunView implements ChoiceCard.ChoiceCardBC {

        private ChoiceCard mChoiceCard;

        public FunView() {

            mChoiceCard = findViewById(R.id.ChoiceCard);
            mChoiceCard.setItemViewHeigth(ViewHelp.getPX(getActivity(), R.dimen.dp100px));
            mChoiceCard.init(false, R.layout.view_choice_card, R.layout.item, R.layout.item, 1);

            mChoiceCard.setChoiceCardBC(this);
            reOnKeyEvent();
            reOnKeyNumberEvent();
        }

        List<FjOrder> fjOrders;

        private void setFjOrders(Collection<FjOrder> getFjOrders) {

            fjOrders = new ArrayList<>(getFjOrders);
            ChoiceItem[] cis = new ChoiceItem[getFjOrders.size()];
            int i = 0;
            for (FjOrder item : getFjOrders) {
                cis[i] = new ChoiceItem("" + i, i + "） 订单号：" + item.fjInfoNo + "  日期：" + item.createTime);
                cis[i].setTag(item);
                i++;
            }

            mChoiceCard.binData(cis);
        }

        private void clickItem(int index) {
            FjOrder fjOrder = null;
            try {
                fjOrder = fjOrders.get(index);
                fjOrder = FjApp.install.getFjWaitOrder().popWaitOrder(fjOrder.fjInfoNo, null);
            } catch (Exception e) {
            }
            if (fjOrder != null) {

                FjApp.install.waitOrder();

                FjOrder finalFjOrder = fjOrder;
//                FjApp.install.getFjInfo().setCurFj(finalFjOrder, finalFjOrder.findNext(null));

                AppConst.mainHandler.post(() -> FjApp.install.getFjInfo().setCurFj(finalFjOrder, finalFjOrder.findNext(null)));

                RouterUINavigation.getInstance().dismiss(FjWaitOrderFragment.this);
            }

        }

        @Override
        public void onChoiceChangeListener(ChoiceCard cc, IChoiceItem c, int index) {
            if (c.getIsChoice()) {
                clickItem(NumberHelp.toInt(c.getID().toString(), -1));
            }
        }


        private void reOnKeyEvent() {
            LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelyardKeyEvent>() {
                @Override
                public Class<OnSteelyardKeyEvent> getBusEventClass() {
                    return OnSteelyardKeyEvent.class;
                }

                @Override
                public void onReceiver(OnSteelyardKeyEvent data) {
                    if (!data.isIntercepted()) {
                        data.setIntercepted(true);

                        if (data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_FUN || data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_FJ)
                            RouterUINavigation.getInstance().dismiss(FjWaitOrderFragment.this);

                    }
                }
            }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
        }

        private void reOnKeyNumberEvent() {

            LinkBusCycleWrapper.register(new ObserverAutoNext<OnKeyNumberEvent>() {
                @Override
                public Class<OnKeyNumberEvent> getBusEventClass() {
                    return OnKeyNumberEvent.class;
                }

                boolean isOnce = false;

                @Override
                public void onReceiver(OnKeyNumberEvent data) {
                    if (data.isIntercepted()) {
                        return;
                    }
                    if (!isOnce && data.isFinally()) {

                        isOnce = true;
                        return;
                    }
                    isOnce = true;
                    if (!data.isFinally() && FjWaitOrderFragment.this.isExit()) {

                        LinkBus.unregisterReceiver(this);
                        return;
                    }
                    data.setIntercepted(true);


                    if (CollectionHelp.getSize(fjOrders) < 10) {
                        if (!data.isFinally()) {
                            clickItem((int) data.getNumber());
                        }
                    } else if (data.isFinally()) {
                        clickItem((int) data.getNumber());
                    }
                }
            }, holdCycleHelp(), CycleHelp.CycleType.Destroy);

        }

    }


}
